package cn.edu.zzuli.wechat.user.controller;

import cn.edu.zzuli.common.constant.RabbitConstant;
import cn.edu.zzuli.common.result.Result;
import cn.edu.zzuli.common.result.ResultInfo;
import cn.edu.zzuli.model.vo.order.AllOfferOrderQueryVo;
import cn.edu.zzuli.model.vo.all.DetailsQueryVo;
import cn.edu.zzuli.wechat.user.service.RunOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "跑腿接单相关接口")
public class RunOrderController {

    @Autowired
    private RunOrderService runOrderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ApiOperation("接受一个订单")
    @GetMapping("/addRunOrder.do")
    public ResultInfo addRunOrder(Integer id, HttpSession session){
        runOrderService.addRunOrder(id,session);
        return Result.ok();
    }

    @ApiOperation("查询接受的订单")
    @GetMapping("/queryRunOrder.do")
    public Map<String, Object> queryRunOrder(AllOfferOrderQueryVo queryVo, HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        Map<String, Object> map = runOrderService.queryRunOrderList(queryVo,uid);
        return map;
    }

    @ApiOperation("按条件查询接受的订单")
    @GetMapping("/queryBYTypeRunOrder.do")
    public Map<String, Object> queryBYTypeRunOrder(AllOfferOrderQueryVo queryVo,HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        Map<String, Object> map = runOrderService.queryRunOrderList(queryVo,uid);
        return map;
    }

    @ApiOperation("查看订单详情")
    @GetMapping("/getDetails.do")
    public ResultInfo getDetails(DetailsQueryVo detailsQueryVo){
        Object res = runOrderService.getDetails(detailsQueryVo);
        return Result.ok(res);
    }

    @ApiOperation("取消一个跑腿的订单")
    @GetMapping("/delRunOrder.do")
    public ResultInfo delRunOrder(int id,HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        runOrderService.delRunOrder(id,0);
        //发送 message : id:订单号 type:类型 uid:当前操作用户
        String message = id + "|2|" + uid ;
        rabbitTemplate.convertAndSend(RabbitConstant.EXCHANGE,RabbitConstant.ROUTING_KEY_CANCEL_ORDER
                ,message);
        return Result.ok();

    }


    @ApiOperation("跑腿者开始完成申请")
    @GetMapping("/applyFinish.do")
    public ResultInfo applyFinish(int id,HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        runOrderService.applyFinish(id,uid);
        return Result.ok();
    }


}
